package com.kristin.net;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.nio.ByteBuffer;
import java.nio.channels.ServerSocketChannel;
import java.nio.channels.SocketChannel;

public class TestMain {
    public static void main(String[] args) throws IOException {
        ServerSocketChannel serverSocketChannel = ServerSocketChannel.open();
        // 绑定端口
        serverSocketChannel.bind(new InetSocketAddress("127.0.0.1",8080));

        // 接收数据
        SocketChannel socketChannel = serverSocketChannel.accept();

        // 读取数据
        ByteBuffer readBuffer = ByteBuffer.allocate(128);

        socketChannel.read(readBuffer);
        readBuffer.flip();
        while(readBuffer.hasRemaining()) {
            System.out.println((char)readBuffer.get());
        }
        socketChannel.close();
        serverSocketChannel.close();
    }

}
